const app = getApp()
const api = app.globalData.api

Page({
    data: {
        funcData: [],
        setting: []
    },
    async getFunc() {
        let res = await api.curlGet('?cla=inventoryOther&fun=func')
        const func = wx.getStorageSync('func'),
            setting = this.getSetting(func)
        if (res.code === 1) {
            this.setData({
                funcData: res.data,
                setting: setting
            })
        } else {
            api.showToast(res.warn)
        }
    },
    getSetting(func) {
        let temp = []
        for (const item of func) {
            temp.push(item.power)
        }
        return temp
    },
    async onChange(obj) {
        const data = obj.currentTarget.dataset
        const key = data.power
        let func = wx.getStorageSync('func') ? wx.getStorageSync('func') : {}
        // 取消按钮
        if (obj.detail === false) {
          func = func.filter(item => item.power !== key);
        } else {
          func.push(data)
        }
        let res = await api.curlPost('?cla=inventoryOther&fun=upDefaultFunc', {'func': api.arrayToJson(func)})
        if (res.code !== 1) {
            api.showToast('设置失败，系统错误', 'none')
        } else {
            wx.setStorageSync('func', func)
            this.setData({
                setting: this.getSetting(func)
            })
        }

    },
    onLoad: async function (options) {
        await this.getFunc()
    }
})
